# coding: utf-8
from typing import Union

from PySide6.QtWidgets import QWidget

from components import SlidingStackedWidget
from .plugins import FileRenameWidget, PluginBase, BiliMergeWidget
from .plugins_list_widget import PluginsListWidget


class ToolsInterface(SlidingStackedWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.pluginsListWidget = PluginsListWidget(self)

        self.__initWidgets()

    def __initWidgets(self):
        self.setObjectName('ToolsInterface')
        self.addWidget(self.pluginsListWidget)

        self.addPlugin(FileRenameWidget(self))
        self.addPlugin(BiliMergeWidget(self))
        self.setCurrentWidget(self.pluginsListWidget)

    def addPlugin(self, plugin_card: Union[PluginBase, QWidget]):
        plugin_card.closed.connect(lambda: self.switchTo(self.pluginsListWidget))
        self.pluginsListWidget.addPlugin(plugin_card, lambda: self.switchTo(plugin_card))
        self.addWidget(plugin_card)

    def switchTo(self, widget: QWidget):
        self.setCurrentWidget(widget)
